package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ThemeContent;
import cn.wolfcode.trip.base.domain.ThemeDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IThemeCatalogService;
import cn.wolfcode.trip.base.service.IThemeDetailService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 礼品主题明细资源控制器
 */
@Controller
@RequestMapping("themeDetail")
public class ThemeDetailController {

    @Autowired
    private IThemeDetailService themeDetailService;

    @Autowired
    private IThemeCatalogService catalogService;
    /**
     * 列表界面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        //按照发布时间降序
        qo.setOrderBy("td.releaseTime desc");
        //查询所有礼品主题目录
        model.addAttribute("catalogs",catalogService.listAll());
        themeDetailService.query(qo).getList().forEach(System.out::println);
        model.addAttribute("pageInfo",themeDetailService.query(qo));
        return "themeDetail/list";
    }

    /**
     * 新增/编辑
     * @param themeDetail
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ThemeDetail themeDetail, MultipartFile file){
        if(file!=null&&file.getSize()>0){
            String url = UploadUtil.uploadQiniu(file);
            themeDetail.setCoverUrl(url);
        }
        themeDetailService.saveOrUpdate(themeDetail);
        return new JsonResult();
    }

    /**
     * 根据明细id查询明细内容
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public ThemeContent getContentById(Long id){
        return themeDetailService.getContentById(id);
    }
}
